﻿using System;
using System.Linq;
using LayerData.DataBaseManager;
using LayerEntity.Sistema;
using Npgsql;

namespace LayerData.Common
{
    public class CommonObjectMaintenance
    {
        #region Variables
        public IPostgreSqlConnectionInfo CurrentConection { get; set; }
        #endregion

        #region Constructor
        public CommonObjectMaintenance(IPostgreSqlConnectionInfo con)
        {
            this.CurrentConection = con;
        }

        #endregion

        #region Funciones
        public int MantenimientoDocumentos(EDocumento documento, EnumOperaciones operacion)
        {
            int proc =(int) PostgreSqlHelper.ExecuteScalar (this.CurrentConection, "common.ISP_GRABAR_SERIES_COMP", documento.ToObject((int)operacion));
            return proc ;
        }
        public string MantenimientosSerisDocs(ESeriesDocumento serie, EnumOperaciones operacion)
        {
            String proc = "";
            bool res = false;
            NpgsqlConnection con = null;
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                NpgsqlTransaction tran = this.CurrentConection.GetTransaction(con);

                proc = (string)PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "COMMON.ISP_GRABAR_SERIES_COMP", serie.ToObject((int)operacion));
                if (proc == "0")
                {
                    res = true;
                }
                else
                {
                    this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                    res = false;
                }
                if (res == true)
                {
                    this.CurrentConection.CommintTransaction(tran);
                }
            }
            catch (NpgsqlException ex)
            {
                Console.Write(ex.Message);
                this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                proc = ex.Message;
                throw;
            }
            finally
            {
                this.CurrentConection.CloseConnection(con);
            }
            return proc;
        }
        public string getCorrelativo(ESeriesDocumento serie)
        {
            String proc = "";
            bool res = false;
            NpgsqlConnection con = null;
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                NpgsqlTransaction tran = this.CurrentConection.GetTransaction(con);
                proc = (string)PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "COMMON.ISP_GET_CORRELATIVO", serie.ToGetCorrelativo());
                this.CurrentConection.CommintTransaction(tran);
            }
            catch (NpgsqlException ex)
            {
                Console.Write(ex.Message);
                this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                proc = ex.Message;
                throw;
            }
            finally
            {
                this.CurrentConection.CloseConnection(con);
            }
            return proc;
        }
        #endregion
    }
}